package com.ydx.meituan.exception;



import com.ydx.meituan.protocol.NetResult;
import com.ydx.meituan.protocol.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


/**
 * Description:异常统一处理
 */
@ControllerAdvice
public class CommonExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public NetResult handleException(Throwable e) {
        logger.error(e.getMessage(),e);
        ExceptionDetail exceptionDetail = new ExceptionDetail();
        if (e instanceof HttpMessageNotReadableException) {//请求体的body没找到
            exceptionDetail.setErrDetail(RequestError.REQUEST_ERROR_PARAMS_ERROR.getMessage());
            return ResultUtils.buildError(new RequestException(RequestError.REQUEST_ERROR_PARAMS_ERROR.getResult(),RequestError.REQUEST_ERROR_PARAMS_ERROR.getMessage(),exceptionDetail));
        } else if (e instanceof RequestException) {
            return ResultUtils.buildError((RequestException) e);
        } else if (e instanceof BindException ){//application/x-www-form-urlencoded
            BindException ex = (BindException) e;
            List<ObjectError> allErrors = ex.getAllErrors();
            ObjectError objectError = allErrors.get(0);
            String message = objectError.getDefaultMessage();
            exceptionDetail.setErrDetail(message);
            return ResultUtils.buildError(new RequestException(RequestError.REQUEST_ERROR_PARAMS_ERROR.getResult(), RequestError.REQUEST_ERROR_PARAMS_ERROR.getMessage(),exceptionDetail));
        }else if (e instanceof MethodArgumentNotValidException ){//application/json
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            BindingResult bindingResult= ex.getBindingResult();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            ObjectError objectError = allErrors.get(0);
            String message = objectError.getDefaultMessage();
            exceptionDetail.setErrDetail(message);
            return ResultUtils.buildError(new RequestException(RequestError.REQUEST_ERROR_PARAMS_ERROR.getResult(), RequestError.REQUEST_ERROR_PARAMS_ERROR.getMessage(),exceptionDetail));
        }
        String message = e.getMessage();
        exceptionDetail.setErrDetail(message);
        return ResultUtils.buildError(new RequestException(RequestError.REQUEST_ERROR_UNKNOWN_ERROR.getResult(), RequestError.REQUEST_ERROR_UNKNOWN_ERROR.getMessage(),exceptionDetail));
    }


}
